package com.hrsystem.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrsystem.entity.*;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.SalaryListDetailMapper;
import com.hrsystem.mapper.SalaryListMapper;
import com.hrsystem.service.SalaryListDetailService;
import com.hrsystem.service.SalaryListService;
import com.hrsystem.service.SalaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class SalaryListDetailController {

    @Autowired
    SalaryListDetailService salaryListDetailService;

    @Autowired
    SalaryListService salaryListService;

    @Autowired
    SalaryService salaryService;

    @Resource
    SalaryListDetailMapper salaryListDetailMapper;



    //查看薪资发放登记详情
    @Transactional
    @GetMapping("/SalaryDistributionListDetail/{id}")
    public Result SalaryDistributionRegistration(@PathVariable(name = "id") Long id){
        QueryWrapper<SalaryListDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sList_id",id);
        List<SalaryListDetail> salaryDistributionDetail = salaryListDetailMapper.selectList(queryWrapper);

        BigDecimal totalSalary = new BigDecimal(0);
        for (SalaryListDetail detail : salaryDistributionDetail) {
            totalSalary = totalSalary.add(detail.getBasic());
            totalSalary = totalSalary.add(detail.getTransportation());
            totalSalary = totalSalary.add(detail.getLunch());
            totalSalary = totalSalary.add(detail.getCommunication());
            totalSalary = totalSalary.add(detail.getEndowment());
            totalSalary = totalSalary.add(detail.getUnemployment());
            totalSalary = totalSalary.add(detail.getMedical());
            totalSalary = totalSalary.add(detail.getProvidentFund());
            if(detail.getBounty()!=null){
                totalSalary = totalSalary.add(detail.getBounty());
            }
            if(detail.getDeduction()!=null){
                totalSalary = totalSalary. subtract(detail.getDeduction());
            }
        }

        // 创建一个包含总额的Map
        Map<String, Object> response = new HashMap<>();
        response.put("salaryDistributionDetail", salaryDistributionDetail);
        response.put("totalSalary", totalSalary);

// 返回包含列表和总额的响应
        return Result.success(response);
    }

    //查看薪资发放登记
    @Transactional
    @GetMapping("/GetSalaryDistributionListDetail")
    public Result SalaryDistributionRegistrationDetail(){
        List<SalaryListDetail> salaryDistributionDetail =salaryListDetailService.list();
        return Result.success(salaryDistributionDetail);
    }

    //查看薪资发放复核详情
    @Transactional
    @GetMapping("/SalaryDistributionReviewListDetail/{id}")
    public Result SalaryDistributionReview(@PathVariable(name = "id") Long id){
        QueryWrapper<SalaryListDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sList_id",id);
        List<SalaryListDetail> salaryDistributionDetail = salaryListDetailMapper.selectList(queryWrapper);
        return Result.success(salaryDistributionDetail);
    }

    //修改
    @Transactional
    @PostMapping("/RewardsAndPunishment")
    public Result RewardsAndPunishment(@RequestBody List<RewardsAndPunishmentItem> rewardsAndPunishmentItems) {
        SalaryListDetail temp = new SalaryListDetail();
        for (RewardsAndPunishmentItem item : rewardsAndPunishmentItems) {

            temp.setFileId(item.getFileId());

            temp=salaryListDetailService.getByFileId(temp.getFileId());

            String workattendance = item.getWorkattendance();
            String workperformance = item.getWorkperformance();
            String workattitude = item.getWorkattitude();
            BigDecimal bounty = new BigDecimal(0);
            BigDecimal deduction = new BigDecimal(0);

// 处理出勤情况
            if ("全勤".equals(workattendance)) {
                bounty = bounty.add(new BigDecimal(400));
            } else if ("有缺勤情况".equals(workattendance)) {
                deduction = deduction.add(new BigDecimal(0));
            } else if ("缺勤较严重".equals(workattendance)) {
                deduction = deduction.add(new BigDecimal(100));
            } else if ("缺勤严重".equals(workattendance)){
                deduction = deduction.add(new BigDecimal(300));
            }

// 处理工作表现
            if ("优秀".equals(workperformance)) {
                bounty = bounty.add(new BigDecimal(1500));
            } else if ("良好".equals(workperformance)) {
                bounty = bounty.add(new BigDecimal(500));
            } else if ("一般".equals(workperformance)) {
                // 无操作
            } else if ("较差".equals(workperformance)) {
                deduction = deduction.add(new BigDecimal(300));
            } else if ("很差".equals(workperformance)){
                deduction = deduction.add(new BigDecimal(500));
            }

// 处理工作态度
            if ("优秀".equals(workattitude)) {
                bounty = bounty.add(new BigDecimal(1500));
            } else if ("良好".equals(workattitude)) {
                bounty = bounty.add(new BigDecimal(500));
            } else if ("一般".equals(workattitude)) {
                // 无操作
            } else if ("较差".equals(workattitude)) {
                deduction = deduction.add(new BigDecimal(300));
            } else if ("很差".equals(workattitude)){
                deduction = deduction.add(new BigDecimal(500));
            }

            System.out.println("Bounty: " + bounty);
            System.out.println("Deduction: " + deduction);
            temp.setBounty(bounty);
            temp.setDeduction(deduction);
            salaryListDetailService.saveOrUpdate(temp);
            System.out.println(item);
        }
        return Result.success(null);
    }

    //修改
    @Transactional
    @PostMapping("/SalaryListadd/{tOrgId}/{fileId}/{name}/{salaryId}")
    public Result SalaryListadd(@PathVariable(name = "tOrgId") Long torgId, @PathVariable(name = "fileId") Long fileId, @PathVariable(name = "name") String name, @PathVariable(name = "salaryId") Long salaryId){
        SalaryListDetail temp;
        temp = new SalaryListDetail();
        BeanUtils.copyProperties(salaryListService.selectSListId(torgId),temp);  //sList
        temp.setFileId(fileId);
        temp.setName(name);
        Salary salary = salaryService.getById(salaryId);
        BeanUtils.copyProperties(salary,temp);  //后续详细薪酬细则
        salaryListDetailService.saveOrUpdate(temp);
        return Result.success(null);
    }


}
